gusucode.com > VC++ 基于IE内核功能很齐全的浏览器(支持多标签)-源码程序 > VC++ 基于IE内核功能很齐全的浏览器(支持多标签)-源码程序/code/Explorer/MainFrm.cpp
//Download by http://www.NewXing.com // MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "SExplore.h" #include "MainFrm.h" #include "ContainerView.h" #include "SourceDialog.h" #include "ViewManView.h" #include "IEView.h" #include "DialogFav.h" typedef UINT (CALLBACK* LPFNORGFAV)(HWND, LPTSTR); typedef BOOL (CALLBACK* LPFNADDFAV)(HWND, TCHAR*, UINT, TCHAR*, UINT, LPITEMIDLIST); #include <urlhist.h> #include <comdef.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() ON_WM_SIZE() ON_WM_CLOSE() ON_COMMAND(ID_BACK, OnBack) ON_COMMAND(ID_FORWARD, OnForward) ON_COMMAND(ID_CUT, OnCut) ON_COMMAND(ID_COPY, OnCopy) ON_COMMAND(ID_PASTE, OnPaste) ON_COMMAND(ID_STOP, OnStop) ON_COMMAND(ID_REFRESH, OnRefresh) ON_COMMAND(ID_HOME, OnHome) ON_COMMAND(ID_SEARCH, OnSearch) ON_COMMAND(ID_PRINTDOC, OnPrintdoc) ON_COMMAND(ID_VIEWSOURCE, OnViewsource) ON_COMMAND(ID_VIEWFONT, OnViewfont) ON_COMMAND(ID_VIEW_FONTS_LARGEST, OnViewFontsLargest) ON_UPDATE_COMMAND_UI(ID_VIEW_FONTS_LARGEST, OnUpdateViewFontsLargest) ON_COMMAND(ID_VIEW_FONTS_LARGE, OnViewFontsLarge) ON_UPDATE_COMMAND_UI(ID_VIEW_FONTS_LARGE, OnUpdateViewFontsLarge) ON_COMMAND(ID_VIEW_FONTS_MEDIUM, OnViewFontsMedium) ON_UPDATE_COMMAND_UI(ID_VIEW_FONTS_MEDIUM, OnUpdateViewFontsMedium) ON_COMMAND(ID_VIEW_FONTS_SMALL, OnViewFontsSmall) ON_UPDATE_COMMAND_UI(ID_VIEW_FONTS_SMALL, OnUpdateViewFontsSmall) ON_COMMAND(ID_VIEW_FONTS_SMALLEST, OnViewFontsSmallest) ON_UPDATE_COMMAND_UI(ID_VIEW_FONTS_SMALLEST, OnUpdateViewFontsSmallest) ON_COMMAND(ID_NEW_WINDOW, OnNewWindow) ON_COMMAND(ID_SHOW_ADRRESS, OnShowAdrress) ON_UPDATE_COMMAND_UI(ID_SHOW_ADRRESS, OnUpdateShowAdrress) ON_COMMAND(ID_SELECT_ALL, OnSelectAll) ON_COMMAND(ID_VIEW_FAVORITY, OnViewFavority) ON_COMMAND(ID_INET_OPTION, OnInetOption) ON_COMMAND(ID_ADDTO_FAV, OnAddtoFav) ON_COMMAND(ID_SETTING_FAV, OnSettingFav) ON_COMMAND(ID_DOC_SAVE_AS, OnDocSaveAs) ON_COMMAND(ID_EDIT_FIND, OnEditFind) ON_COMMAND(ID_CLOSE_WINDOW, OnCloseWindow) ON_COMMAND(ID_CLOSE_ALL, OnCloseAll) ON_COMMAND(ID_SHOWTITLE, OnShowtitle) ON_UPDATE_COMMAND_UI(ID_SHOWTITLE, OnUpdateShowtitle) ON_COMMAND(ID_SHOW_MAINWND, OnShowMainwnd) ON_COMMAND(ID_SETDEFAULTID, OnSetdefaultid) ON_UPDATE_COMMAND_UI(ID_BACK, OnUpdateBack) ON_UPDATE_COMMAND_UI(ID_FORWARD, OnUpdateForward) ON_COMMAND(ID_SETACTIVENEW, OnSetActiveNewWnd) ON_UPDATE_COMMAND_UI(ID_SETACTIVENEW, OnUpdateSetActiveNewWnd) ON_UPDATE_COMMAND_UI(ID_SETDEFAULTID, OnUpdateSetdefaultid) //}}AFX_MSG_MAP ON_MESSAGE(WM_NEW_IEVIEW,OnCreateNewIEView) ON_COMMAND(IDOK, OnNewAddressEnter) ON_CBN_SELENDOK(AFX_IDW_TOOLBAR + 1,OnNewAddress) ON_CBN_EDITCHANGE(AFX_IDW_TOOLBAR + 1,OnAddressChange) ON_COMMAND_RANGE(0xe00, 0xfff, OnFavorite) ON_NOTIFY(TBN_DROPDOWN, AFX_IDW_TOOLBAR, OnDropDown) ON_MESSAGE(WM_COPYDATA,OnCopyDataFromApp) END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here NONCLIENTMETRICS ncm; memset(&ncm, 0, sizeof(NONCLIENTMETRICS)); ncm.cbSize = sizeof(NONCLIENTMETRICS); VERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0)); m_fontAddress.CreateFontIndirect(&ncm.lfMessageFont); m_strMainWndText=AfxGetApp()->GetProfileString("","MainTitle","波子浏览器"); m_bActvieNew=AfxGetApp()->GetProfileInt("","ActiveNewWnd",1); m_bShowTitle=AfxGetApp()->GetProfileInt("","ShowTitle",1); m_nFontSize=21; m_pAutoCmpAddress=NULL; m_pEnumSting=NULL; m_pAutoEnumString=NULL; CString strBkBmFile=AfxGetApp()->GetProfileString("","BkBitmap",""); strBkBmFile.TrimLeft(); strBkBmFile.TrimRight(); if(!strBkBmFile.IsEmpty()) { HBITMAP hbm=NULL; hbm=(HBITMAP)::LoadImage(NULL,strBkBmFile, IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE); m_bmBarBm.Attach(hbm); } else { m_bmBarBm.LoadBitmap(IDB_BMBAR); } } CMainFrame::~CMainFrame() { if(m_pAutoCmpAddress) m_pAutoCmpAddress->Release(); if(m_pEnumSting) m_pEnumSting->Release(); m_fontAddress.DeleteObject(); if(m_bmBarBm.GetSafeHandle()) m_bmBarBm.DeleteObject(); } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndMenuBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP, CRect(0,0,0,0), 10002) || !m_wndMenuBar.LoadMenu(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD|WS_VISIBLE|CBRS_ALIGN_TOP, CRect(0,0,0,0), AFX_IDW_TOOLBAR) || !m_wndToolBar.LoadToolBar(IDR_NAVBAR)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } // set up toolbar properties m_wndToolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS); CString str; CImageList imageList; CBitmap bitmap; // Create and set the normal toolbar image list. bitmap.LoadBitmap(IDB_COOLTOOL); imageList.Create(22, 20, ILC_COLORDDB|ILC_MASK, 13, 1); imageList.Add(&bitmap, RGB(255,0,255)); m_wndToolBar.GetToolBarCtrl().SetImageList(&imageList); imageList.Detach(); bitmap.Detach(); // Create and set the hot toolbar image list. bitmap.LoadBitmap(IDB_HOTTOOL); imageList.Create(22, 20, ILC_COLORDDB|ILC_MASK, 13, 1); imageList.Add(&bitmap, RGB(255,0,255)); m_wndToolBar.GetToolBarCtrl().SetHotImageList(&imageList); imageList.Detach(); bitmap.Detach(); CRect rectToolBar; // set up toolbar button sizes m_wndToolBar.GetItemRect(0, &rectToolBar); m_wndToolBar.SetSizes(rectToolBar.Size(), CSize(22,20)); // add dropdown button. /* m_wndToolBar.AddDropDownButton(ID_BACK, IDR_DUMMY, TRUE); m_wndToolBar.AddDropDownButton(ID_FORWARD, IDR_DUMMY, TRUE); */ m_wndToolBar.SetButtonInfo(14,ID_VIEWFONT, TBSTYLE_BUTTON | TBSTYLE_DROPDOWN, 8); m_wndToolBar.SetButtonInfo(16,ID_VIEW_FAVORITY, TBSTYLE_BUTTON | TBSTYLE_DROPDOWN, 12); //m_wndToolBar.SetButtonInfo(18,ID_SHOWTITLE, TBSTYLE_BUTTON,11); if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } if (!m_wndAddress.Create(CBS_DROPDOWN|CBS_AUTOHSCROLL|WS_CHILD, CRect(0, 0, 400, 120), this, AFX_IDW_TOOLBAR + 1)) { TRACE0("Failed to create combobox\n"); return -1; // fail to create } m_wndAddress.LimitText(0); InitAddress(); // create the animation control m_wndAnimate.Create(WS_CHILD|WS_VISIBLE , CRect(0, 0, 25, 25), this, AFX_IDW_TOOLBAR + 2); // the AVI should be 26x26 pixels :O) // test if there is an avi in the current directory available m_wndAnimate.Open(IDR_NAVBAR); m_wndAnimate.Play(0,-1,-1); if (!m_wndReBar.Create(this) || !m_wndReBar.AddBar(&m_wndMenuBar,NULL,&m_bmBarBm,RBBS_GRIPPERALWAYS) || !m_wndReBar.AddBar(&m_wndAnimate,NULL,&m_bmBarBm,RBBS_FIXEDBMP | RBBS_FIXEDSIZE) || !m_wndReBar.AddBar(&m_wndToolBar,NULL,&m_bmBarBm,RBBS_FIXEDBMP | RBBS_BREAK) || !m_wndReBar.AddBar(&m_wndAddress,_T("地址:"),&m_bmBarBm, RBBS_FIXEDBMP | RBBS_BREAK)) { TRACE0("Failed to create rebar\n"); return -1; // fail to create } // TODO: Remove this if you don't want tool tips m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY); m_wndMenuBar.SetBarStyle(m_wndMenuBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY); m_wndReBar.SetBarStyle(m_wndReBar.GetBarStyle() | CBRS_BORDER_ANY ); m_wndTabCtrl.Create("TabCtrl","",WS_CHILD|WS_VISIBLE|FTS_HASARROWS|TCS_TOOLTIPS|FTS_HASARROWS, CRect(0,0,0,0), &m_wndStatusBar,ID_TABBAR); SetWindowPlace(); m_wndSysTray.Create(m_strMainWndText,AfxGetApp()->LoadIcon(IDI_SYSTRAY),IDM_SYSTRAY); m_wndSysTray.SetMenuDefaultItem(0); // RegisterWindowMessage(WM_SHELL_OPEN_FILE); return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs //注册窗口类 WNDCLASS wndcls; GetClassInfo(AfxGetInstanceHandle(),cs.lpszClass,&wndcls); wndcls.lpszClassName=_T(MAIN_WND_CLASS_NAME); wndcls.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); if (!AfxRegisterClass(&wndcls)) { return FALSE; } cs.lpszClass=_T(MAIN_WND_CLASS_NAME); cs.style&=~FWS_ADDTOTITLE; cs.style&=~FWS_PREFIXTITLE; return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers void CMainFrame::SetWindowPlace() { WINDOWPLACEMENT wpl; wpl.length=sizeof(WINDOWPLACEMENT); wpl.ptMaxPosition.x=AfxGetApp()->GetProfileInt("Placement","MaxX",600); wpl.ptMaxPosition.y=AfxGetApp()->GetProfileInt("Placement","MaxY",600); wpl.ptMinPosition.x=AfxGetApp()->GetProfileInt("Placement","MinX",30); wpl.ptMinPosition.y=AfxGetApp()->GetProfileInt("Placement","MinY",30); wpl.rcNormalPosition.left=AfxGetApp()->GetProfileInt("Placement","Left",30); wpl.rcNormalPosition.top=AfxGetApp()->GetProfileInt("Placement","Top",30); wpl.rcNormalPosition.right=AfxGetApp()->GetProfileInt("Placement","Right",640); wpl.rcNormalPosition.bottom=AfxGetApp()->GetProfileInt("Placement","Bottom",500); wpl.showCmd=AfxGetApp()->GetProfileInt("Placement","ShowCmd",SW_SHOWMAXIMIZED); wpl.flags=AfxGetApp()->GetProfileInt("Placement","Flags",0); SetWindowPlacement(&wpl); } void CMainFrame::SaveWindowPlace() { if(IsIconic()) return; WINDOWPLACEMENT wpl; wpl.length=sizeof(WINDOWPLACEMENT); GetWindowPlacement(&wpl); AfxGetApp()->WriteProfileInt("Placement","MaxX",wpl.ptMaxPosition.x); AfxGetApp()->WriteProfileInt("Placement","MaxY",wpl.ptMaxPosition.y); AfxGetApp()->WriteProfileInt("Placement","MinX",wpl.ptMinPosition.x); AfxGetApp()->WriteProfileInt("Placement","MinY",wpl.ptMinPosition.y); AfxGetApp()->WriteProfileInt("Placement","Left",wpl.rcNormalPosition.left); AfxGetApp()->WriteProfileInt("Placement","Top",wpl.rcNormalPosition.top); AfxGetApp()->WriteProfileInt("Placement","Right",wpl.rcNormalPosition.right); AfxGetApp()->WriteProfileInt("Placement","Bottom",wpl.rcNormalPosition.bottom); AfxGetApp()->WriteProfileInt("Placement","ShowCmd",wpl.showCmd); AfxGetApp()->WriteProfileInt("Placement","Flags",wpl.flags); } BOOL CMainFrame::AddHistory(LPCTSTR lpszURL, LPCTSTR lpszTitle) { #if 0 HRESULT hr; IUrlHistoryStg2 *puhs; _bstr_t bstrUrl(lpszURL); _bstr_t bstrTitle(lpszURL); hr = ::CoCreateInstance(CLSID_CUrlHistory, NULL, CLSCTX_INPROC_SERVER, IID_IUrlHistoryStg, (LPVOID *)&puhs); if(SUCCEEDED(hr)) { hr=puhs->AddUrl(bstrUrl,bstrTitle,NULL); ASSERT(hr==S_OK); puhs->Release(); /* COMBOBOXEXITEM item; item.mask = CBEIF_TEXT; item.iItem = -1; // insert at the end. So the order is keeped item.pszText = (LPTSTR)lpszURL; m_wndAddress.InsertItem(&item); */ m_pAutoEnumString->AddRef(); m_pAutoEnumString->AddString(lpszURL); m_pAutoEnumString->Release(); } #endif return TRUE; } void CMainFrame::SetFontSizeVal(int nVal) { m_nFontSize=nVal; } void CMainFrame::SetWindowTextX(LPCTSTR lpszText) { CString strText; if(m_bShowTitle) { strText=lpszText; strText+=" - "; strText+=m_strMainWndText; } else { strText =" -- "; strText+=m_strMainWndText; strText+=" -- "; } SetWindowText(strText); } BOOL CMainFrame::DeleteCurIEView() { CWnd* pWnd=GetWindow(GW_CHILD); ASSERT(pWnd->IsKindOf(RUNTIME_CLASS(CContainerView))); CViewManView *pView=((CContainerView*)pWnd)->m_pViewman; if(m_wndTabCtrl.GetItemCount()>1) { int nItemSelect=m_wndTabCtrl.GetCurSel(); if(pView->DeleteView(nItemSelect)) { m_wndTabCtrl.DeleteItem(nItemSelect); m_wndTabCtrl.SetItemSel(pView->GetActiveView()); /* if(nItemSelect>0) { m_wndTabCtrl.SetCurSel(nItemSelect-1); pView->SetActiveView(nItemSelect-1); } else { m_wndTabCtrl.SetCurSel(0); pView->SetActiveView(0); } */ } } else { m_wndTabCtrl.DeleteItem(0); pView->DeleteView(0); SendMessage(WM_NEW_IEVIEW); ((CContainerView*)pWnd)->GetAciveView()->Navigate2("about:blank",NULL,0); return FALSE; } return TRUE; } BOOL CMainFrame::InitAddress() { typedef HRESULT (CALLBACK* LPFNDLLFUNC1)(HWND ,DWORD); HINSTANCE hIns = LoadLibrary("shlwapi.dll"); if(hIns != NULL) { LPFNDLLFUNC1 lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hIns, "SHAutoComplete"); if(lpfnDllFunc1!=NULL) lpfnDllFunc1(m_wndAddress.GetEditCtrl()->m_hWnd, 0xe); FreeLibrary(hIns); } /* HRESULT hr; IUrlHistoryStg2 *puhs; IEnumSTATURL *pesu; STATURL su; ULONG celt; _bstr_t bstr; hr = ::CoCreateInstance(CLSID_CUrlHistory, NULL, CLSCTX_INPROC_SERVER, IID_IUrlHistoryStg, (LPVOID *)&puhs); m_pAutoEnumString=new CAutoEnumString; m_pAutoEnumString->AddRef(); if(SUCCEEDED(hr)) { hr = puhs->EnumUrls(&pesu); if(SUCCEEDED(hr)) { while(SUCCEEDED(pesu->Next(1, &su, &celt)) && celt > 0) { bstr = su.pwcsUrl; CString strAddress; strAddress=(LPCTSTR)(bstr); if(strAddress.Find("http://",0)==0)//有选择的添加 { // m_wndAddress.AddString(bstr); if(strAddress.GetLength()<30) { COMBOBOXEXITEM item; item.mask = CBEIF_TEXT; item.iItem = -1; // insert at the end. So the order is keeped item.pszText = (LPTSTR)(LPCTSTR)bstr; m_wndAddress.InsertItem(&item); } } m_pAutoEnumString->AddString(bstr); } puhs->Release(); } } //自动完成功能的添加 m_pAutoEnumString->QueryInterface(IID_IEnumString,(LPVOID*)&m_pEnumSting); m_pAutoEnumString->Release(); ::CoCreateInstance( CLSID_AutoComplete, NULL, CLSCTX_INPROC_SERVER, IID_IAutoComplete, (LPVOID *) &m_pAutoCmpAddress ); HWND hwnd= (HWND)m_wndAddress.SendMessage(CBEM_GETEDITCONTROL); if(hwnd) { m_pAutoCmpAddress->Init(hwnd,m_pEnumSting,NULL,NULL); } IAutoComplete2 *pac2; if (SUCCEEDED(m_pAutoCmpAddress->QueryInterface(IID_IAutoComplete2, (LPVOID*)&pac2))) { pac2->SetOptions(ACO_AUTOSUGGEST|ACO_AUTOAPPEND); pac2->Release(); } */ return TRUE; } CIEView* CMainFrame::GetActiveIEView() { CWnd* pWnd=GetWindow(GW_CHILD); ASSERT(pWnd->IsKindOf(RUNTIME_CLASS(CContainerView))); return ((CContainerView*)pWnd)->GetAciveView(); } void CMainFrame::OnSize(UINT nType, int cx, int cy) { CFrameWnd::OnSize(nType, cx, cy); // TODO: Add your message handler code here if (m_wndStatusBar.GetSafeHwnd() && m_wndTabCtrl.GetSafeHwnd()) { m_wndStatusBar.SetPaneInfo(1,m_wndStatusBar.GetItemID(1),SBPS_STRETCH,cx*3/5); CRect rc; m_wndStatusBar.GetItemRect(1, rc); //0 m_wndTabCtrl.SetWindowPos(NULL,rc.left,rc.top,rc.Width(), rc.Height()+1, SWP_NOZORDER); } if(IsIconic()) { ShowWindow(SW_HIDE); } } BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->message==WM_LBUTTONUP) { //处理单击动画关闭窗口 CPoint pt; ::GetCursorPos(&pt); CRect rc; m_wndAnimate.GetWindowRect(&rc); if(rc.PtInRect(pt)) { DeleteCurIEView(); } } return CFrameWnd::PreTranslateMessage(pMsg); } BOOL CMainFrame::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { // TODO: Add your specialized code here and/or call the base class if(ID_TABBAR == (UINT)wParam) { NMHDR* pNMHDR = (NMHDR*)lParam; switch(pNMHDR->code) { case TCN_SELCHANGING: break; case TCN_SELCHANGE: { int nIndex=m_wndTabCtrl.GetCurSel(); CWnd* pWnd=GetWindow(GW_CHILD); ASSERT(pWnd->IsKindOf(RUNTIME_CLASS(CContainerView))); ((CContainerView*)pWnd)->SetActiveView(nIndex); m_wndAddress.SetWindowText(GetActiveIEView()->GetLocationURL()); SetWindowTextX(GetActiveIEView()->m_strTitle); COleVariant vaZoomFactor; GetActiveIEView()->ExecWB(OLECMDID_ZOOM,OLECMDEXECOPT_DONTPROMPTUSER,NULL,&vaZoomFactor); SetFontSizeVal(vaZoomFactor.intVal*10+1); } break; } } /* if(AFX_IDW_TOOLBAR==(UINT)wParam) { //AfxMessageBox("aa"); NMHDR* pNMHDR = (NMHDR*)lParam; if(pNMHDR->code==TBN_DROPDOWN) { ButtonDropDown(pNMHDR,pResult); } } */ return CFrameWnd::OnNotify(wParam, lParam, pResult); } LRESULT CMainFrame::OnCreateNewIEView(WPARAM wParam,LPARAM lParam) { int nOldActive=m_wndTabCtrl.GetCurSel(); CWnd* pWnd=GetWindow(GW_CHILD); ASSERT(pWnd->IsKindOf(RUNTIME_CLASS(CContainerView))); CIEView* pView=((CContainerView*)pWnd)->CreateIEView(); if(pView) { ((CContainerView*)pWnd)->AddTabItem("请稍候……",(CWnd*)pView); if(m_bActvieNew) { ((CContainerView*)pWnd)->SetActiveView(m_wndTabCtrl.GetItemCount()-1); m_wndTabCtrl.SetCurSel(m_wndTabCtrl.GetItemCount()-1); } else { //((CContainerView*)pWnd)->SetActiveView(nOldActive); //m_wndTabCtrl.SetCurSel(nOldActive); } return (LRESULT)pView; } else { return NULL; } } void CMainFrame::OnClose() { // TODO: Add your message handler code here and/or call default SaveWindowPlace(); //保存是否显示标题 AfxGetApp()->WriteProfileInt("","ShowTitle",m_bShowTitle); AfxGetApp()->WriteProfileInt("","ActiveNewWnd",m_bActvieNew); CFrameWnd::OnClose(); } void CMainFrame::OnBack() { // TODO: Add your command handler code here GetActiveIEView()->GoBack(); } void CMainFrame::OnForward() { // TODO: Add your command handler code here GetActiveIEView()->GoForward(); } void CMainFrame::OnCut() { // TODO: Add your command handler code here if(GetFocus()==m_wndAddress.GetEditCtrl()) { CBBClipboard clipb; CString strText; int nStart,nEnd; m_wndAddress.GetEditCtrl()->GetSel(nStart,nEnd); m_wndAddress.GetEditCtrl()->GetWindowText(strText); strText=strText.Mid(nStart,nEnd); clipb.SetText((LPSTR)(LPCTSTR)strText); m_wndAddress.GetEditCtrl()->ReplaceSel("",TRUE); } else { GetActiveIEView()->ExecWB(OLECMDID_CUT, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL); } } void CMainFrame::OnCopy() { // TODO: Add your command handler code here if(m_wndAddress.GetFocus()==m_wndAddress.GetEditCtrl()) { CBBClipboard clipb; CString strText; int nStart,nEnd; m_wndAddress.GetEditCtrl()->GetSel(nStart,nEnd); m_wndAddress.GetEditCtrl()->GetWindowText(strText); strText=strText.Mid(nStart,nEnd); clipb.SetText((LPSTR)(LPCTSTR)strText); } else { GetActiveIEView()->ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL); } } void CMainFrame::OnPaste() { // TODO: Add your command handler code here if(m_wndAddress.GetFocus()==m_wndAddress.GetEditCtrl()) { CBBClipboard clipb; char szText[1024]; clipb.GetText(szText,1023); m_wndAddress.GetEditCtrl()->ReplaceSel(szText,TRUE); } else { GetActiveIEView()->ExecWB(OLECMDID_PASTE, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL); } } void CMainFrame::OnStop() { // TODO: Add your command handler code here GetActiveIEView()->Stop(); } void CMainFrame::OnRefresh() { // TODO: Add your command handler code here GetActiveIEView()->Refresh(); } void CMainFrame::OnHome() { // TODO: Add your command handler code here GetActiveIEView()->GoHome(); } void CMainFrame::OnSearch() { // TODO: Add your command handler code here GetActiveIEView()->GoSearch(); } void CMainFrame::OnPrintdoc() { // TODO: Add your command handler code here LPOLECOMMANDTARGET pCmdTarg = NULL; LPDISPATCH m_pDisp = GetActiveIEView()->GetHtmlDocument(); //get the IDispatch interface pointer if(m_pDisp==NULL) return; m_pDisp->QueryInterface(IID_IOleCommandTarget, (LPVOID*)&pCmdTarg); //query for olecommandtarget interface ASSERT(pCmdTarg); pCmdTarg->Exec(NULL, //call the olecommandtarget's Exec method OLECMDID_PRINT, 0, NULL, NULL ); if (pCmdTarg) pCmdTarg->Release(); // release document's command target if (m_pDisp) m_pDisp->Release(); // release document's dispatch interfac } void CMainFrame::OnViewsource() { // TODO: Add your command handler code here MSHTML::IHTMLDocument2Ptr ptrDoc; CSourceDialog *dlg; ptrDoc = GetActiveIEView()->GetHtmlDocument(); dlg = new CSourceDialog; dlg->Create(IDD_SOURCE); dlg->ShowSource(ptrDoc); dlg->ShowWindow(SW_SHOW); } void CMainFrame::OnViewfont() { // TODO: Add your command handler code here } void CMainFrame::OnViewFontsLargest() { // TODO: Add your command handler code here COleVariant vaZoomFactor(4l); m_nFontSize=41; GetActiveIEView()->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, &vaZoomFactor, NULL); } void CMainFrame::OnUpdateViewFontsLargest(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetRadio(m_nFontSize==41); } void CMainFrame::OnViewFontsLarge() { // TODO: Add your command handler code here COleVariant vaZoomFactor(3l); m_nFontSize=31; GetActiveIEView()->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, &vaZoomFactor, NULL); } void CMainFrame::OnUpdateViewFontsLarge(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetRadio(m_nFontSize==31); } void CMainFrame::OnViewFontsMedium() { // TODO: Add your command handler code here COleVariant vaZoomFactor(2l); m_nFontSize=21; GetActiveIEView()->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, &vaZoomFactor, NULL); } void CMainFrame::OnUpdateViewFontsMedium(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetRadio(m_nFontSize==21); } void CMainFrame::OnViewFontsSmall() { // TODO: Add your command handler code here COleVariant vaZoomFactor(1l); m_nFontSize=11; GetActiveIEView()->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, &vaZoomFactor, NULL); } void CMainFrame::OnUpdateViewFontsSmall(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetRadio(m_nFontSize==11); } void CMainFrame::OnViewFontsSmallest() { // TODO: Add your command handler code here COleVariant vaZoomFactor(0l); m_nFontSize=01; GetActiveIEView()->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, &vaZoomFactor, NULL); } void CMainFrame::OnUpdateViewFontsSmallest(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetRadio(m_nFontSize==01); } void CMainFrame::OnNewWindow() { // TODO: Add your command handler code here SendMessage(WM_NEW_IEVIEW); } void CMainFrame::OnShowAdrress() { // TODO: Add your command handler code here m_wndAddress.ShowWindow(m_wndAddress.IsWindowVisible()?SW_HIDE:SW_SHOW); RecalcLayout(); } void CMainFrame::OnUpdateShowAdrress(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetCheck(m_wndAddress.IsWindowVisible()?1:0); } void CMainFrame::OnFavorite(UINT nID) { CString strAddress=m_astrFavoriteURLs.ElementAt(nID-0xe00); GetActiveIEView()->ReadUrlDocument((LPCTSTR)strAddress); } void CMainFrame::OnSelectAll() { // TODO: Add your command handler code here GetActiveIEView()->ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL); } void CMainFrame::OnViewFavority() { // TODO: Add your command handler code here static CDialogFav dlg; if (dlg.GetSafeHwnd() == 0) { TCHAR sz[MAX_PATH]; TCHAR szPath[MAX_PATH]; HKEY hKey; DWORD dwSize; // find out from the registry where the favorites are located. if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"), &hKey) != ERROR_SUCCESS) { TRACE0("Favorites folder not found\n"); return; } dwSize = sizeof(sz); RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize); ExpandEnvironmentStrings(sz, szPath, MAX_PATH); RegCloseKey(hKey); dlg.m_DefaultPath.Format("%s",szPath); dlg.Create(IDD_DIALOGFAV); } dlg.ShowWindow(SW_SHOW); } void CMainFrame::OnDropDown(NMHDR* pNotifyStruct, LRESULT* pResult) { ButtonDropDown(pNotifyStruct,pResult); } int CMainFrame::BuildFavoritesMenu(LPCTSTR pszPath, int nStartPos, CMenu *pMenu) { CString strPath(pszPath); CString strPath2; CString str; WIN32_FIND_DATA wfd; HANDLE h; int nPos; int nEndPos; int nNewEndPos; int nLastDir; TCHAR buf[INTERNET_MAX_PATH_LENGTH]; CStringArray astrFavorites; CStringArray astrDirs; CMenu* pSubMenu; if(strPath[strPath.GetLength() - 1] != _T('\\')) strPath += _T('\\'); strPath2 = strPath; strPath += "*.*"; // 查找目录,首先查找.URL文件,然后查找子目录中的.URL文件 h = FindFirstFile(strPath, &wfd); if(h != INVALID_HANDLE_VALUE) { nEndPos = nStartPos; do { if((wfd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))==0) { str = wfd.cFileName; if(str.Right(4) == _T(".url")) { ::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"), _T(""), buf, INTERNET_MAX_PATH_LENGTH, strPath2 + str); str = str.Left(str.GetLength() - 4); for(nPos = nStartPos ; nPos < nEndPos ; ++nPos) { if(str.CompareNoCase(astrFavorites[nPos]) < 0) break; } astrFavorites.InsertAt(nPos, str); m_astrFavoriteURLs.InsertAt(nPos, buf); ++nEndPos; } } } while(FindNextFile(h, &wfd)); FindClose(h); // 把查到的选项增加到菜单中 for(nPos = nStartPos ; nPos < nEndPos ; ++nPos) { pMenu->AppendMenu(MF_STRING | MF_ENABLED, 0xe00 + nPos, astrFavorites[nPos]); } //已经处理完所有的.URL文件,下面查找子目录 nLastDir = 0; h = FindFirstFile(strPath, &wfd); ASSERT(h != INVALID_HANDLE_VALUE); do { if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // 忽略当前目录和父目录项 if(lstrcmp(wfd.cFileName, _T(".")) == 0 || lstrcmp(wfd.cFileName, _T("..")) == 0) continue; for(nPos = 0 ; nPos < nLastDir ; ++nPos) { if(astrDirs[nPos].CompareNoCase(wfd.cFileName) > 0) break; } pSubMenu = new CMenu; pSubMenu->CreatePopupMenu(); //递归调用函数 nNewEndPos = BuildFavoritesMenu(strPath2 + wfd.cFileName, nEndPos, pSubMenu); if(nNewEndPos != nEndPos) { //只插入有.URL文件的子目录到菜单中 nEndPos = nNewEndPos; pMenu->InsertMenu(nPos, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT)pSubMenu->m_hMenu, wfd.cFileName); pSubMenu->Detach(); astrDirs.InsertAt(nPos, wfd.cFileName); ++nLastDir; } delete pSubMenu; } } while(FindNextFile(h, &wfd)); FindClose(h); } return nEndPos; } void CMainFrame::OnNewAddressEnter() { CString str; m_wndAddress.GetWindowText(str); if (str.GetLength() == 0) return; GetActiveIEView()->ReadUrlDocument(str); } void CMainFrame::OnAddressChange() { } void CMainFrame::OnNewAddress() { CString str; m_wndAddress.GetLBText(m_wndAddress.GetCurSel(), str); if (str.GetLength() == 0) return; GetActiveIEView()->ReadUrlDocument(str); } void CMainFrame::OnInetOption() { // TODO: Add your command handler code here IShellDispatch* disp; HRESULT hr; hr = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER, IID_IDispatch, (LPVOID*)&disp ) ; if (FAILED (hr)) { CString str; str.Format(_T("Failed to create Instance :-( ")); TRACE( str) ; return; } hr=disp->ControlPanelItem(_bstr_t("inetcpl.cpl")); disp->Release(); } void CMainFrame::OnAddtoFav() { // TODO: Add your command handler code here IShellUIHelper* pShellUIHelper; HRESULT hr = CoCreateInstance(CLSID_ShellUIHelper, NULL, CLSCTX_INPROC_SERVER, IID_IShellUIHelper, (LPVOID*)&pShellUIHelper); if (SUCCEEDED(hr)) { CIEView* pView = GetActiveIEView(); _variant_t vtTitle(pView->GetTitle().AllocSysString()); CString strURL = pView->GetLocationURL(); hr = pShellUIHelper->AddFavorite(strURL.AllocSysString(), &vtTitle); pShellUIHelper->Release(); } } void CMainFrame::OnSettingFav() { // TODO: Add your command handler code here HMODULE hMod = (HMODULE)LoadLibrary("shdocvw.dll"); if (hMod) { LPFNORGFAV lpfnDoOrganizeFavDlg = (LPFNORGFAV)GetProcAddress(hMod, "DoOrganizeFavDlg"); if (lpfnDoOrganizeFavDlg) { TCHAR szPath[MAX_PATH]; HRESULT hr = SHGetSpecialFolderPath(m_hWnd, szPath, CSIDL_FAVORITES, TRUE); if (SUCCEEDED(hr)) { lpfnDoOrganizeFavDlg(m_hWnd, szPath); } } FreeLibrary(hMod); } } void CMainFrame::OnDocSaveAs() { // TODO: Add your command handler code here COleVariant bstr; GetActiveIEView()->ExecWB(OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT, &bstr, NULL); } void CMainFrame::OnEditFind() { // TODO: Add your command handler code here MSHTML::IHTMLDocument2Ptr ptrDoc; _variant_t varInput; _variant_t varOutput; IOleCommandTarget *pCMD; HRESULT hr; ptrDoc = GetActiveIEView()->GetHtmlDocument(); hr = ptrDoc.QueryInterface(IID_IOleCommandTarget, &pCMD); if(SUCCEEDED(hr)) { varInput = _T(""); hr = pCMD->Exec(&CGID_MSHTML, IDM_FIND, OLECMDEXECOPT_DODEFAULT, &varInput, &varOutput); } } void CMainFrame::OnCloseWindow() { // TODO: Add your command handler code here DeleteCurIEView(); } void CMainFrame::OnCloseAll() { // TODO: Add your command handler code here while(DeleteCurIEView()); } void CMainFrame::OnShowtitle() { // TODO: Add your command handler code here if(m_bShowTitle) { m_bShowTitle=0; } else { m_bShowTitle=1; } } void CMainFrame::OnUpdateShowtitle(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetCheck(m_bShowTitle); } void CMainFrame::OnShowMainwnd() { // TODO: Add your command handler code here if(!IsWindowVisible()) { if(IsIconic()) { ShowWindow(SW_RESTORE); } else { ShowWindow(SW_SHOW); } SetForegroundWindow(); } else { ShowWindow(SW_HIDE); } } void CMainFrame::OnSetdefaultid() { // TODO: Add your command handler code here GetActiveIEView()->SetRegisterAsBrowser(TRUE); } void CMainFrame::OnUpdateSetdefaultid(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetCheck(GetActiveIEView()->GetRegisterAsBrowser()); } void CMainFrame::EnableGoBack(BOOL bEnable) { m_bEnableGoBack=bEnable; } void CMainFrame::EnableGoForward(BOOL bEnable) { m_bEnableGoForward=bEnable; } void CMainFrame::OnUpdateBack(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable(m_bEnableGoBack); } void CMainFrame::OnUpdateForward(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable(m_bEnableGoForward); } LRESULT CMainFrame::OnCopyDataFromApp(WPARAM wParam,LPARAM lParam) { PCOPYDATASTRUCT pcs=(PCOPYDATASTRUCT)lParam; char *szUrl=(char*)pcs->lpData; LRESULT ret=SendMessage(WM_NEW_IEVIEW); CIEView* pView=(CIEView*)ret; if(pView) pView->ReadUrlDocument(szUrl); return 1; } void CMainFrame::ButtonDropDown(NMHDR* pNotifyStruct, LRESULT* pResult) { // this function handles the dropdown menus from the toolbar NMTOOLBAR* pNMToolBar = (NMTOOLBAR*)pNotifyStruct; CRect rect; // translate the current toolbar item rectangle into screen coordinates // so that we'll know where to pop up the menu m_wndToolBar.GetToolBarCtrl().GetRect(pNMToolBar->iItem, &rect); rect.top = rect.bottom; ::ClientToScreen(pNMToolBar->hdr.hwndFrom, &rect.TopLeft()); if(pNMToolBar->iItem == ID_VIEWFONT) { CMenu menu; CMenu* pPopup; // the font popup is stored in a resource menu.LoadMenu(IDR_FONT_POPUP); pPopup = menu.GetSubMenu(0); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, rect.left, rect.top + 1, AfxGetMainWnd()); } if(pNMToolBar->iItem == ID_VIEW_FAVORITY) { CMenu menu; CMenu* pPopup=NULL; // the font popup is stored in a resource menu.LoadMenu(IDR_FONT_POPUP); pPopup = menu.GetSubMenu(1); // 建立收藏夹菜单 TCHAR sz[MAX_PATH]; TCHAR szPath[MAX_PATH]; HKEY hKey; DWORD dwSize; m_astrFavoriteURLs.RemoveAll(); while(pPopup->DeleteMenu(0, MF_BYPOSITION)); // 从注册表中查找收藏夹内容 if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"), &hKey) != ERROR_SUCCESS) { TRACE0("Favorites folder not found\n"); return; } dwSize = sizeof(sz); RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize); ExpandEnvironmentStrings(sz, szPath, MAX_PATH); RegCloseKey(hKey); //建立收藏夹菜单 BuildFavoritesMenu(szPath, 0, pPopup); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, rect.left, rect.top + 1, AfxGetMainWnd()); } *pResult = TBDDRET_DEFAULT; } void CMainFrame::OnSetActiveNewWnd() { // TODO: Add your command handler code here if(m_bActvieNew) { m_bActvieNew=0; } else { m_bActvieNew=1; } } void CMainFrame::OnUpdateSetActiveNewWnd(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->SetCheck(m_bActvieNew); }